package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcProduceService;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 生产领料
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/prd_pick")
@RestController
public class XqcPrdPickController {

    /** 生产服务 */
    @Autowired
    private IXqcProduceService produceService;

    /**
     * 保存生产领料单(到审核阶段)
     */
    @PostMapping("/save_user_prd_pick")
    @VtuzxApi
    public Object saveUserPrdPick(@VtuzxControlParam VtuzxToken token, @SuppressWarnings("unused") @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return produceService.saveUserPrdPick( _userId, name, param);

    }

    /**
     * 添加用户临时生产领料物料箱号
     */
    @PostMapping("/add_user_prdpick_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
    } )
    public Object addUserPrdPickMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        int count = VtuzxUtil.getInt(param, "count");
        return produceService.addUserPrdPickMaterialBox(_userId, name, stockNo, boxNo, count);
    }

    /**
     * 添加用户临时其他出库物料箱号
     */
    @PostMapping("/add_user_prdpick_material_box_list")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "boxList", label = "箱号列表", notNull = true),
    } )
    public Object addUserPrdPickMaterialBoxList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        List<Map<String, Object>> boxList = VtuzxUtil.getObject(param, "boxList");
        return produceService.addUserPrdPickMaterialBoxList(_userId, name, stockNo, boxList);
    }

    /**
     * 删除用户临时生产领料物料箱号
     */
    @PostMapping("/delete_user_prdpick_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "id", notNull = true)
    } )
    public Object deleteUserPrdPickMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        return produceService.deleteUserPrdPickMaterialBox(_userId, id);
    }

    /**
     * 删除用户临时生产领料物料
     */
    @PostMapping("/delete_user_prdpick_material")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "物料行id", notNull = true)
    } )
    public Object deleteUserPrdPickMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        produceService.deleteUserPrdPickMaterial(_userId, id);
        return null;
    }

    /**
     * 删除用户临时生产领料单(清空)
     */
    @PostMapping("/delete_user_prdpick")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object deleteUserPrdPick(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        produceService.deleteUserPrdPick(_userId, stockNo);
        return null;
    }

    /**
     * 编辑用户临时生产领料物料箱号
     */
    @PostMapping("/update_user_prdpick_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "materialNo", label = "物料编码", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
            @VtuzxValidator(key = "count", label = "数量", notNull = true, isNumber = true)
    } )
    public Object updateUserPrdPickMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String materialNo = VtuzxUtil.getString(param, "materialNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        double count = VtuzxUtil.getDouble(param, "count");
        return produceService.updateUserPrdPickMaterialBox(_userId, stockNo, materialNo, boxNo, count);
    }

    /**
     * 获取用户临时生产领料物料列表
     */
    @PostMapping("/get_user_prdpick_material")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object getUserPrdPickMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        return produceService.getUserPrdPickMaterial(_userId, stockNo);
    }

    /**
     * 获取用户临时生产领料物料箱号列表
     */
    @PostMapping("/get_user_prdpick_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "_detailId", label = "detailId", notNull = true),
    } )
    public Object getUserPrdPickMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String _detailId = VtuzxUtil.getString(param, "_detailId");
        return produceService.getUserPrdPickMaterialBox(_userId, _detailId);
    }

    /**
     * 获取生产领料单
     */
    @PostMapping("/get_prdpick_bill")
    @VtuzxApi
    public Object getPrdPickBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, "pageIndex");
        int pageSize = VtuzxUtil.getInt(param, "pageSize");
        return produceService.getPrdPickBill(_userId, param, pageIndex, pageSize);
    }

    /**
     * 获取生产领料单分录列表
     */
    @PostMapping("/get_prdpick_bill_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object getPrdPickBillDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return produceService.getPrdPickBillDetail(_userId, stockNo, billNo);
    }

    /**
     * 获取生产领料单分录的箱号详细
     */
    @PostMapping("/get_prdpick_bill_detail_box")
    @VtuzxApi
    public Object getPrdPickBillDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String _detailId = VtuzxUtil.getString(param, "_detailId");
        return produceService.getPrdPickBillDetailBox(_userId, _detailId);
    }
}
